package com.scisoft.scan.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.TextView;

import com.scisoft.scan.data.BarCodeBean;
import com.scisoft.scan.data.PartBean;
import com.scisoft.scan.db.PartDBHelper;
import com.scisoft.scan.dy.R;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CommonUtil {

    private static Dialog progressDialog = null;

    /**
     * 时间戳转成提示性日期格式（昨天、今天……)
     */
    public static String getDateToString(Date date) {
        SimpleDateFormat format;
        String hintDate = "";
        //先获取年份
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
        //获取一年中的第几天
        int day = Integer.valueOf(new SimpleDateFormat("d").format(date));
        //获取当前年份 和 一年中的第几天
        Date currentDate = new Date(System.currentTimeMillis());
        int currentYear = Integer.valueOf(new SimpleDateFormat("yyyy").format(currentDate));
        int currentDay = Integer.valueOf(new SimpleDateFormat("d").format(currentDate));
        //计算 如果是去年的
        if (currentYear - year == 1) {
            //如果当前正好是 1月1日 计算去年有多少天，指定时间是否是一年中的最后一天
            if (currentDay == 1) {
                int yearDay;
                if (year % 400 == 0) {
                    yearDay = 366;//世纪闰年
                } else if (year % 4 == 0 && year % 100 != 0) {
                    yearDay = 366;//普通闰年
                } else {
                    yearDay = 365;//平年
                }
                if (day == yearDay) {
                    hintDate = "昨天";
                }
            }
        } else {
            if (currentDay - day == 1) {
                hintDate = "昨天";
            }
            if (currentDay - day == 0) {
                hintDate = "今天";
            }
        }
        if (TextUtils.isEmpty(hintDate)) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return format.format(date);
        } else {
            format = new SimpleDateFormat("HH:mm");
            return hintDate + " " + format.format(date);
        }

    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 显示提示框
     */
    public static void showPress(Context context) {
        progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.style_dialog);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = progressDialog.findViewById(R.id.id_tv_loadingmsg);
        msg.setText("加载中......");
        progressDialog.show();
    }

    /**
     * 隐藏提示框
     */
    public static void hidePress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }

    }

    /**
     * 隐藏提示框
     */
    public static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return null;
        }
        if (strEndIndex < 0) {
            return null;
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }

    /**
     * 解析二维码，处理成为配件或者货架
     */
    public static BarCodeBean ParseBarCodeNew(Context context,String barCode) {

        BarCodeBean barCodeBean = new BarCodeBean();
        try {
            if (barCode.startsWith("https://r.qpyun.cn?a=hj")) {
                //汽配云格式的货架
                //货架格式：https://r.qpyun.cn?a=hj&id=65135&c=G01-03-01&x=90144468
                String temp = subString(barCode, "&c=", "&x=");
                if (temp != null) {
                    barCodeBean.barType = BarCodeBean.BAR_SHELVES;
                    barCodeBean.shelvesBean.shelvesName = temp;
                }
            } else if (barCode.startsWith("TSPHJ=")) {
                //TSP的货架
                //货架格式：TSPHJ==G01-03-01&ID=90144468
                String temp = subString(barCode, "TSPHJ=", "&ID=");
                if (temp != null) {
                    barCodeBean.barType = BarCodeBean.BAR_SHELVES;
                    barCodeBean.shelvesBean.shelvesName = temp;
                }
            } else if (barCode.endsWith("999999")) {
                //TSP格式的无码件
                String temp = barCode.substring(0, barCode.indexOf("SSSSSS999999"));
                barCodeBean.barType = BarCodeBean.BAR_PART;
                barCodeBean.partBean.partType = PartBean.TSP;
                barCodeBean.partBean.partCode = temp;
            } else if (barCode.length() > 12) {
                //TSP格式的有码件
                String serialNo = barCode.substring(barCode.length() - 6);//最后六位是唯一码
                String supplierCode_old = barCode.substring(barCode.length() - 12, barCode.length() - 6);//倒数第二个6位是供应商
                String supplierCode_new = barCode.substring(barCode.length() - 16, barCode.length() - 6);//倒数第二个10位是供应商，供应商以0000开头或者HR开头
                String supplierCode = "";
                String partNo = "";
                int isNew = 0;
                if (supplierCode_new != null && (supplierCode_new.startsWith("HR") || supplierCode_new.startsWith("0000"))) {
                    //优先判别是否是新码
                    PartDBHelper dbHelper = new PartDBHelper(context, "partinfo.db", null, 1);
                    //符合新码规则的，与本地库比较，是否存在特殊的0000的旧码
                    String testoldNo = barCode.substring(0, barCode.length() - 12);
                    if (dbHelper.queryPartCount(testoldNo) > 0) {
                        //如果按照旧码识别，并且库里面有，那么就是旧码
                        supplierCode = supplierCode_old;
                        partNo = testoldNo;
                        isNew = 0;
                    } else {
                        //如果按照旧码识别，库里没有的，就是新码
                        supplierCode = supplierCode_old;//供应商码还是取原来的
                        partNo = barCode.substring(0, barCode.length() - 16);
                        isNew = 1;
                    }
                } else {
                    //不是新码则认定为旧码
                    supplierCode = supplierCode_old;
                    partNo = barCode.substring(0, barCode.length() - 12);
                    isNew = 0;
                }
                //仅配件代码、唯一码、供应商代码有效
                barCodeBean.barType = BarCodeBean.BAR_PART;
                barCodeBean.partBean.partType = PartBean.TSP;
                barCodeBean.partBean.partNo = partNo;
                barCodeBean.partBean.partCode = partNo;
                barCodeBean.partBean.serialNo = serialNo;
                barCodeBean.partBean.supplierCode = supplierCode;
            }
        } catch (Exception e) {
            barCodeBean = new BarCodeBean();
            barCodeBean.barType = BarCodeBean.BAR_UNKNOWN;
        }
        return barCodeBean;
    }

    public static byte[] BitmapToBytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        return baos.toByteArray();
    }

}
